package com.demo.schedule.regular;

import com.alibaba.fastjson.JSON;
import com.demo.entity.DvtDiagnose;
import com.demo.entity.Patient;
import com.demo.entity.VtePreventive;
import com.demo.mapper.DvtDiagnoseMapper;
import com.demo.mapper.PatientMapper;
import com.demo.mapper.VtePreventiveMapper;
import com.demo.schedule.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
@DependsOn("springContextUtil")
@Order(2)
@Component
public class RegularMindSchedule {

    private static ApplicationContext applicationContext;
    public static ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);

    @Resource
    private VtePreventiveMapper vtePreventiveMapper;
    @Resource
    private DvtDiagnoseMapper dvtDiagnoseMapper;

    //@PostConstruct
    public void initRegularMindSchedule() {
        applicationContext = SpringContextUtil.getApplicationContext();
        // vte：查询患者评估类型为定期提醒的评估集合[根据病人分组，取最新的一条定期评估记录]
        List<VtePreventive> vtePreventives = vtePreventiveMapper.queryRecentVtePreventive();
        for (VtePreventive preventive : vtePreventives) {
            VtePreventive vtePreventive = vtePreventiveMapper.selectById(preventive.getVtePreventiveId());
            if (vtePreventive == null) {
                continue;
            }
            addVteTask(vtePreventive);
        }

        // dvt 筛查定时提醒
        List<DvtDiagnose> dvtDiagnoses = dvtDiagnoseMapper.queryRecentDvtDiagnose();
        for (DvtDiagnose diagnose : dvtDiagnoses) {
            DvtDiagnose dvtDiagnose = dvtDiagnoseMapper.selectById(diagnose.getDvtDiagnoseId());
            if (dvtDiagnose == null) {
                continue;
            }
            addDvtTask(dvtDiagnose);
        }

    }

    /**
     * 添加vte定时提醒任务
     * @param vtePreventive
     */
    public synchronized static void addVteTask(VtePreventive vtePreventive) {
        log.info("添加 vte 定时提醒任务");
        log.info("vtePreventive 评估记录 {}", JSON.toJSONString(vtePreventive));
        Integer preventiveRiskRank = vtePreventive.getPreventiveRiskRank();
        Long wakeupMinute = null;
        if (preventiveRiskRank.intValue() == 1 || preventiveRiskRank.intValue() == 4) { // 1-低危, 4-极低危
            log.info("低风险、极低风险，每周提醒一次");
            wakeupMinute = RegularMindUtil.getVteRiskRankLevel1Or4(vtePreventive);
        } else if (preventiveRiskRank.intValue() == 3) {//3-中危
            log.info("中危，每周提醒两次（周一、周四：周二、周五；周三、周六；周日：周三、周六）");
            wakeupMinute = RegularMindUtil.getVteRiskRankLevel3(vtePreventive);
        } else if (preventiveRiskRank.intValue() == 2) {//2-高危
            log.info("高危，每天提醒");
            wakeupMinute = RegularMindUtil.getVteRiskRankLevel2(vtePreventive);
        }
        Patient patient = applicationContext.getBean(PatientMapper.class).selectById(vtePreventive.getPatientId());
        log.info("vte 定时提醒 {} 分钟后触发", wakeupMinute);
        executor.schedule(new RegularVteMindThread(patient, vtePreventive, applicationContext), wakeupMinute, TimeUnit.MINUTES);
    }

    /**
     * 添加dvt定时提醒任务
     * @param dvtDiagnose
     */
    public synchronized static void addDvtTask(DvtDiagnose dvtDiagnose) {
        log.info("添加 dvt 定时提醒任务");
        log.info("dvtDiagnose 评估记录 {}", JSON.toJSONString(dvtDiagnose));
        Long wakeupMinute = RegularMindUtil.getDvtRemind(dvtDiagnose);
        Patient patient = applicationContext.getBean(PatientMapper.class).selectById(dvtDiagnose.getPatientId());
        log.info("dvt 定时提醒 {} 分钟后触发", wakeupMinute);
        executor.schedule(new RegularDvtMindThread(patient, dvtDiagnose, applicationContext), wakeupMinute, TimeUnit.MINUTES);
    }

}
